package top.hmtools.makedata;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import top.hmtools.makedata.common.ClassUtils;
import top.hmtools.makedata.common.ResourceUtils;

public class MakeData {

	final static Logger logger = LoggerFactory.getLogger(MakeData.class);
	

	static {
		try {
//			 String packageString = "org.apache.commons.io";
			String packageString = "top.hmtools.makedata";
			packageString = packageString.replaceAll("\\.", "/");
			Enumeration<URL> resources = ClassUtils.getURLs(packageString);
			while (resources.hasMoreElements()) {
				URL url = resources.nextElement();
				logger.info(">>>>>>>>>>>>>>>{}", url);
				
				boolean isFileURL = ResourceUtils.isFileURL(url);
				boolean isJarURL = ResourceUtils.isJarURL(url);
				
				logger.info("isFileURL：{}，isJarURL：{}",isFileURL,isJarURL);

				if (isFileURL) {
					File file = FileUtils.toFile(url);
					File[] listFiles = file.listFiles();
					for (File item : listFiles) {
						System.out.println(item);
					}
				}else if(isJarURL){
					URLConnection urlConnection = url.openConnection();
					JarURLConnection jarURLConnection = (JarURLConnection)urlConnection;
					JarFile jarFile = jarURLConnection.getJarFile();
					Enumeration<JarEntry> entries = jarFile.entries();
					while(entries.hasMoreElements()){
						JarEntry jarEntry = entries.nextElement();
						if(jarEntry.isDirectory()){
							continue;
						}
						String className = jarEntry.getName().replaceAll("/", ".");
						if(!className.toUpperCase().endsWith(".CLASS")){
							continue;
						}
						className = className.substring(0, className.length()-6);
						try {
							Class<?> clazz = Class.forName(className);
							Object object = clazz.newInstance();
							
							logger.info(">>>>>>>>>>>>>>>{}",object);
						} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 获取指定class类 自身及其所有父类的所有属性
	 * 
	 * @param clazz
	 * @return
	 */
	public static Field[] getFieldsOfClass(Class<? extends Object> clazz) {
		return MakeData.getFieldsOfClass(clazz, null);
	}

	/**
	 * 获取指定class类 自身及其所有父类的所有属性
	 * 
	 * @param clazz
	 * @param fields
	 * @return
	 */
	public static Field[] getFieldsOfClass(Class<? extends Object> clazz, Field[] fields) {
		// 获取父类的所有属性
		Class<?> superclass = clazz.getSuperclass();
		if (superclass != null) {
			fields = MakeData.getFieldsOfClass(superclass, fields);
		}

		if (fields == null || fields.length < 1) {
			// 获取当前class的所有属性
			fields = clazz.getDeclaredFields();
		} else {
			// 追加当前class的父类所有属性
			Field[] current = clazz.getDeclaredFields();
			Field[] temp = new Field[fields.length + current.length];
			System.arraycopy(fields, 0, temp, 0, fields.length);
			System.arraycopy(current, 0, temp, fields.length, current.length);
			fields = temp;
		}
		return fields;
	}
}
